<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage PluginsFunction
 */
/**
 * Smarty {counter} function plugin
 *
 * Type: function<br>
 * Name: counter<br>
 * Purpose: print out a counter value
 *
 * @author Monte Ohrt <monte at ohrt dot com>
 * @link http://www.smarty.net/manual/en/language.function.counter.php {counter}
 * (Smarty online manual)
 * @param array $params
 * parameters
 * @param Smarty_Internal_Template $template
 * template object
 * @return string null
 */
function smarty_function_list($params, $template)
{
   
    $limitNum = isset($params['num']) ? $params['num'] : 10;
    if (isset($params['sort'])) {
        $order = $params['sort'];
    } else {
        $order = 'id';
    }
    // 增加flag标签 属于某个范围 全局调用
    if (isset($params['flag']) && isset($params['cid'])) {
        $rs = NewsModel::instance()->getNewsByFlag($params['flag'], $limitNum, $params['cid'], $order);
        $ok = 1;
    } elseif (isset($params['flag']) && isset($params['fid'])) {
        // 先获取子类  2013-4-15 修正flag问题
        $ids = getSonCate($params['fid']);
        if (empty($ids)) {
            throw new Exception('the category  has no son!');
        }
        $count = 0;
        //   $ids = str_replace('-', ',', $cates['topid']);
        //   $ids = substr($ids, 0, strlen($ids) - 1);
        $rs = NewsModel::instance()->getNewsByFlag($params['flag'], $limitNum, $ids, $order);
        // $rs = NewsModel::instance()->getNewsListByIds($ids, $limitNum, $order);
        $ok = 1;
    } elseif (isset($params['flag']) && ! isset($params['cid']) && ! isset($params['fid'])) {
        // 调用全局
        $rs = NewsModel::instance()->getNewsByFlag($params['flag'], $limitNum, NULL, $order);
        $ok = 1;
    }
    if ($ok == 1) {
        //重置flag
        $flag = NewsModel::instance()->getFlag();
        $arrFlag = array();
        foreach ($flag as $v) {
            $arrFlag[$v['flag']] = $v['name'];
        }
        // 查找扩展字段
        $rs = getExtend($rs);
        
        // 如果没有将调用他的子类
        $template->assign($params['assign'], $rs);
    }
    // 获取制定分类的内容
    if (isset($params['cid']) && ! isset($params['flag'])) {
        $sort = empty($params['sort']) ? 'date' : $params['sort'];
        $rs = NewsModel::instance()->getNewsByWhere(array(
                'cid' => $params['cid']), null, $limitNum, $sort);
        if (empty($rs)) {
            Benchmark::print_r('no data!');
        }
        // 查找扩展字段
        // 查找扩展字段
        

        $rs = getExtend($rs);
        // 如果没有将调用他的子类
        $template->assign($params['assign'], $rs);
    }
    // 获取制定分类的内容
    if (isset($params['fid']) && ! isset($params['flag'])) {
        // 先获取子类
        $ids = getSonCate($params['fid']);
        if (empty($ids)) {
            throw new Exception('the category  has no son!');
        }
        
        //   $ids = substr($ids, 0, strlen($ids) - 1);
        $rs = NewsModel::instance()->getNewsListByIds($ids, $limitNum, $order);
        if (empty($rs)) {
            Benchmark::print_r('no data!');
        }
        // 查找扩展字段
        $rs = getExtend($rs);
        // 控制条数
        // 如果没有将调用他的子类
        $template->assign($params['assign'], $rs);
    }
}
function getSonCate($id = 1)
{
    
    $rs = CategoryModel::instance()->sort2array($id);
    foreach ($rs as $v) {
        //回收站
        if ($v['id'] == 1067) {
            continue;
        }
        $newids .= $v['Sort']['id'] . ',';
    }
    $newids = substr($newids, 0, strlen($newids) - 1);
    return $newids;

}
function getExtend($rs)
{
    foreach ($rs as $k => $v) {
        
        $extend = array();
        $extend1 = array();
        $extend2 = array();
        $extend3 = array();
        $extend1 = NewsModel::instance()->getExtendVarcharByWhere($v['id'], 1);
        $extend2 = NewsModel::instance()->getExtendIntByWhere($v['id'], 1);
        
        $extend3 = NewsModel::instance()->getExtendDecimalByWhere($v['id'], 1);
        
        $extend = array_merge($extend1, $extend2, $extend3);
        if (! empty($v['flag'])) {
            $rs[$k]['flag'] = explode(',', $v['flag']);
        }
        // 重组
        foreach ($extend as $ev) {
            $rs[$k][$ev['key']] = $ev['value'];
        }
    }
    return $rs;

}
?>